import {onMounted, reactive, ref} from 'vue';
import {queryCmsmwList, queryInvmbList, queryIPage} from "@api/url/erp/cmsmv";
import router from '@/router';
import notification from "@arco-design/web-vue/es/notification";
import {QmsmgDel, QmsmgUpdate} from "@api/url/erp/baseMessage";
export default function importConfig() {
  const base = reactive<any>({
    checkForm: {
        ph: '',
        fs: '',
        gy: ''
    },
    loadingData: false,
    pagination: {
        defaultPageSize: 10,
        total: 1, //总条数
        current: 1,     // 页码 必传
        size: 10,
        showTotal: true,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 40, 50],
    },
    tableData: [],
    delVisible:false,
    currentInfo:{},
    visible:false,
    emptyForm:{},
    form:{},
    type:'number',
    formItems:[],
    formItems1: [
      {
        label: '品号',
        name: 'mg002',
      },
      {
        label: '品名',
        name: 'udf10',
      },
      {
        label: '规格',
        name: 'udf11',
      },
      {
        label: '检验类别',
        name: 'mg020',
        isSelect: true,
        options: [{name:"调机首检",value:'调机首检'},{name:'首检',value:'首检'},{name:"来料检验",value:'来料检验'},{name:'过程检验',value:'过程检验'},{name:"成品检验",value:'成品检验'}],
      },
      {
        label: '工艺',
        name: 'udf09',
      },
      {
        label: '工艺说明',
        name: 'udf08',
      },
      {
        label: '文字/数字',
        name: 'mg011',
        isSelect: true,
        options: [{name:"数字型",value:'3'},{name:'文字型',value:'4'}],
      },
      {
        label: '检验编号',
        name: 'mg003',
        disabled:true,
      },
      {
        label: '检验项目',
        name: 'udf01',
      },
      {
        label: '检验工具',
        name: 'udf02',
      },
      {
        label: '检验频次',
        name: 'udf03',
      },
      {
        label: '品管类别',
        name: 'mg001',
      },
      {
        label: '检验标准',
        name: 'mg006',
      },
      {
        label: '合格判定项目',
        name: 'mg010',
        isSelect: true,
        options: [{name:"Y",value:'Y'},{name:'N',value:'N'}],
      },
      {
        label: '正公差',
        name: 'mg012',
        isInputNumber: true,
        precision:3,
        min:0,
        step:0.001,
      },
      {
        label: '负公差',
        name: 'mg013',
        isInputNumber: true,
        precision:3,
        step:0.001,
      },
      {
        label: '类型',
        name: 'mg009',
        isSelect: true,
        options: [{name:"计数",value:'1'},{name:'计量',value:'2'}],
      },
      {
        label: '抽样数量',
        name: 'mg016',
        isInputNumber: true,
        precision:0,
        min:0,
      },
      {
        label: '累计破坏数量',
        name: 'mg017',
        isSelect: true,
        options: [{name:"Y",value:'Y'},{name:'N',value:'N'}],
      },
      {
        label: '抽样规则',
        name: 'mg015',
        isSelect: true,
        options: [{name:"依抽查基础",value:'1'},{name:'定数抽样',value:'2'}],
      },
      {
        label: '检验场别',
        name: 'udf06',
        isSelect: true,
        options: [{name:"现场",value:'现场'},{name:'测量室',value:'测量室'}],
      },
      {
        label: '备注',
        name: 'mg007',
      },
    ],
    formItems2:[],
    rules: {
      mg002: [
        {
          required: true,
          message: '品号不可为空',
        },
      ],
      udf09: [
        {
          required: true,
          message: '工艺不可为空',
        },
      ],
      mg020: [
        {
          required: true,
          message: '检验类别不可为空',
        },
      ],
    },
    cpvisible:false,
    cpLimitParam: {
      current: 1,
      size: 10,
      total: 0,
      ph: '',
      pm: ''
    },
    cpLoading:false,
    tableCpData:[],
    gyvisible:false,
    gyLimitParam: {
      current: 1,
      size: 10,
      total: 0,
      gy: ''
    },
    gyLoading:false,
    tableGyData:[],
    formRef:null,
  });
  // tableCpRef:null,
  // tableGyRef:null,
  // 选择时间
  const onChange = (val) => {
    base.checkForm.startTime = val[0].replaceAll('-','');
    base.checkForm.endTime = val[1].replaceAll('-','');
  }


  // 查询
  const selectDataListFun = () => {
    getData();
  };

  // 获取数据
  const getData = () => {
    base.loadingData = true;
    const param = {
      current: base.pagination.current,
      size: base.pagination.size,
      ph: base.checkForm.ph,
      fs: base.checkForm.fs,
      gy: base.checkForm.gy
    };
    queryIPage(param).then((res: any) => {
      if(res.code === 200){
        base.tableData = res.data.data;
        base.pagination.total = res.data.total;
        console.log(base.tableData);
      }else{
        notification.error(res.message);
        base.tableData = [];
      };
      base.loadingData = false;
    });
  };

  getData();
  //
  const getTableData = (val) => {
    base.pagination.current = val.currentPage;
    base.pagination.size = val.pageSize;
    getData();
  }


  /**
   * @exports viewDetails 查看详情单
   * @param param 品号
   */

  // 重置
  const resetData = () => {
    base.checkForm.ph = '';
    base.checkForm.fs = '';
    base.checkForm.gy = '';
    getData();
  }

  // 跳转新增检验
  const goProductInspectionPage = () => {
    router.push("/productInspection");
  }

  //编辑
  const edit=(row)=>{
    base.currentInfo=row;
    base.formRef.resetFields();
    base.form=JSON.parse(JSON.stringify(row));
    base.type=row.mg011=='3'?'number':row.mg011=='4'?'text':'';
    if(row.mg011=='3'){
      base.formItems=JSON.parse(JSON.stringify(base.formItems1));
    }else{
      base.formItems=JSON.parse(JSON.stringify(base.formItems2));
    }

    base.visible=true;
  };

  //删除
  const del=(row)=>{
    base.currentInfo=row;
    base.delVisible=true;
  };

  // 确定删除
  const delHandleOk = () => {
    console.log(base.currentInfo);
    let obj={
      mg002:base.currentInfo.mg002,
      mg003:base.currentInfo.mg003,
    };

    QmsmgDel(obj).then(res => {
      notification.info(res.data);
      if(res.code==200){
        getData();
      }
    });
  };

  //确定修改
  const handleOk=()=>{
    base.formRef.validate((obj) => {
      if (!obj) {
        QmsmgUpdate(base.form).then(res=>{
          if(res.code==200){
            notification.info(res.data);
            base.visible=false;
            base.pagination.current = 1;
            base.pagination.size = 10;
            getData();
          }else{
            notification.info(res.message);
            base.visible=false;
          }

        });
      }
    });
  };

  // 加载产品
  const getInvmbList = () => {
    base.cpLoading = true;
    const param = {
      current: base.cpLimitParam.current,
      size: base.cpLimitParam.size,
      ph: base.cpLimitParam.ph,
      pm: base.cpLimitParam.pm
    };
    queryInvmbList(param).then((res: any) => {
      // console.log(res);
      if(res.code === 200){
        base.tableCpData = res.data.data;
        base.cpLimitParam.total = res.data.total;
      }else{
        base.tableCpData = [];
        notification.error(res.message);
      };
      base.cpLoading = false;
    });
  };

  //弹窗内获取焦点事件
  const getFocus=(label)=>{
    if(label=='品号'||label=='品名'||label=='规格'){
      base.cpvisible=true;
    }else if(label=='工艺'||label=='工艺说明'){
      base.gyvisible=true;
    }
  };

  // 产品分页
  const getCpTableData = (val) => {
    base.cpLimitParam.current = val.currentPage;
    base.cpLimitParam.size = val.pageSize;
    getInvmbList();
  };

  // 查询产品
  const queryCpFun = () => {
    base.cpLimitParam.current = 1;
    getInvmbList();
  };

  // 加载工艺
  const getCmsmwList = () => {
    base.gyLoading = true;
    const param = {
      current: base.gyLimitParam.current,
      size: base.gyLimitParam.size,
      gy: base.gyLimitParam.gy
    };
    queryCmsmwList(param).then((res: any) => {
      if(res.code === 200){
        base.tableGyData = res.data.data;
        base.gyLimitParam.total = res.data.total;
      }else{
        base.tableGyData = [];
        notification.error(res.message);
      }
      base.gyLoading = false;
    })

  };

  // 查询工艺
  const queryGyFun = () => {
    base.gyLimitParam.current = 1;
    getCmsmwList();
  };

  // 工艺分页
  const getGyTableData = (val) => {
    base.gyLimitParam.current = val.currentPage;
    base.gyLimitParam.size = val.pageSize;
    getCmsmwList();
  }

  // 工艺表格
  const tableGyRef = ref();

  // 确认选择工艺
  const gyhandleOk = () => {
    getRadioEventGy(tableGyRef);
  };

  // 获取选中
  const getRadioEvent = (elm) => {
    const $table = elm.value
    if ($table) {
      const currRow = $table.getRadioRecord();
      base.form.mg002 = currRow.mb001;
      base.form.udf10 = currRow.mb002;
      base.form.udf11 = currRow.mb003;
    };
  };

  // 产品表格
  const tableCpRef = ref();

  // 确认选择产品
  const cphandleOk = () => {
    getRadioEvent(tableCpRef);
  };

  // 获取选中
  const getRadioEventGy = (elm) => {
    const $table = elm.value
    if ($table) {
      const currRow = $table.getRadioRecord()
      base.form.mg008 = currRow.mw001;
      base.form.udf09 = currRow.mw002;
      base.form.udf08 = currRow.mw003;
    };
  };

  //切换弹窗选项
  const changeOption=(name)=>{
    if(name=='mg011'){
      base.type=base.form.mg011=='3'?'number':base.form.mg011=='4'?'text':'';
      if(base.form.mg011=='3'){
        base.formItems=JSON.parse(JSON.stringify(base.formItems1));
      }else{
        base.formItems=JSON.parse(JSON.stringify(base.formItems2));
      }
    }
  };


  onMounted(()=>{
    base.emptyForm=JSON.parse(JSON.stringify(base.form));
    let arr=JSON.parse(JSON.stringify(base.formItems1));
    arr.splice(14, 3);
    base.formItems2=JSON.parse(JSON.stringify(arr));

    getInvmbList();
    getCmsmwList();
  });

  return {
    base,
    onChange,
    selectDataListFun,
    getTableData,
    resetData,
    goProductInspectionPage,
    edit,
    del,
    delHandleOk,
    handleOk,
    getFocus,
    queryCpFun,
    getCpTableData,
    gyhandleOk,
    queryGyFun,
    getGyTableData,
    cphandleOk,
    tableCpRef,
    tableGyRef,
    changeOption,
  };
}
